package com.jxwifi.cloud.quickcleanserver.utils;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.jxwifi.cloud.quickcleanserver.R;


/**
 * 
 * @author yiyi
 * @date 2015年12月10日
 */
public class NaviContext {
	private SparseArray<INavigation> mNaviPool;
	public final static int Navi_baidu = 0;
	public final static int Navi_gaode = 1;
	public final static int Navi_baidu_web = 2;
	private String lng, lat;

	public NaviContext(SparseArray<INavigation> navis, String lng, String lat) {
		if (navis == null || navis.size() < 3) {
			throw new RuntimeException("导航对象池初始化错误");
		}
		this.mNaviPool = navis;
		this.lng = lng;
		this.lat = lat;
	}

	/**
	 * 
	 * @return 如果没进入导航页面，说明还有经过选择步骤
	 */
	public boolean startAutonavi(Context context) {

		if (DetectionUtils.isAvilible(context, "com.baidu.BaiduMap")
				&& DetectionUtils.isAvilible(context, "com.autonavi.minimap")) {
			return false;
		} else if (DetectionUtils.isAvilible(context, "com.baidu.BaiduMap")) {
			mNaviPool.get(Navi_baidu).startAutonavi(context, lng, lat);
		} else if (DetectionUtils.isAvilible(context, "com.autonavi.minimap")) {
			mNaviPool.get(Navi_gaode).startAutonavi(context, lng, lat);
		} else {
			mNaviPool.get(Navi_baidu_web).startAutonavi(context, lng, lat);
		}
		return true;
	}

	/**
	 * 安装了多个地图就需要选择
	 */
	public void need2Select(final Activity context, View v) {
		DisplayMetrics metric = new DisplayMetrics();
		context.getWindowManager().getDefaultDisplay().getMetrics(metric);
		int height = metric.heightPixels;

		View root_memberid = LayoutInflater.from(context).inflate(R.layout.service_mapview_choose, new LinearLayout(context),false);

		final PopupWindow popup_paymember = DetectionUtils.PopupWindows.getPoputWindowHeight(root_memberid, v, height);

		ImageView mapview_choose_delete = (ImageView) root_memberid.findViewById(R.id.mapview_choose_delete);
		RelativeLayout mapview_choose_map_gaode_rela = (RelativeLayout) root_memberid
				.findViewById(R.id.mapview_choose_map_gaode_rela);
		RelativeLayout mapview_choose_map_baidu_rela = (RelativeLayout) root_memberid
				.findViewById(R.id.mapview_choose_map_baidu_rela);
		LinearLayout center_orderpart_paymemberid_Lin1 = (LinearLayout) root_memberid
				.findViewById(R.id.center_orderpart_paymemberid_Lin1);
		LinearLayout center_orderpart_paymemberid_Lin2 = (LinearLayout) root_memberid
				.findViewById(R.id.center_orderpart_paymemberid_Lin2);
		mapview_choose_delete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				popup_paymember.dismiss();
			}
		});
		// 高德地图
		mapview_choose_map_gaode_rela.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mNaviPool.get(Navi_gaode).startAutonavi(context, lng, lat);
			}
		});
		// 百度地图
		mapview_choose_map_baidu_rela.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mNaviPool.get(Navi_baidu).startAutonavi(context, lng, lat);
			}
		});
		center_orderpart_paymemberid_Lin1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				popup_paymember.dismiss();
			}
		});
		center_orderpart_paymemberid_Lin2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				popup_paymember.dismiss();
			}
		});

		popup_paymember.showAtLocation(v, Gravity.CENTER, 0, height);
	}


}
